package com.kingyea.mobilepolice.kim.task;


import com.kingyea.mobilepolice.kim.service.KimUserFriendsService;
import com.kingyea.mobilepolice.kim.utils.ConnectionDB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * KIM 定时任务
 *
 * @author lxz
 * @version v1.0.0
 * @description
 * @date Created in 2018/9/13 16:55
 */
@Component
@EnableScheduling
public class KimSynPlanTask {

    private Logger logger = LoggerFactory.getLogger(KimSynPlanTask.class);

    @Value("${kim-uam.mysql.driver}")
    private String driver;
    @Value("${kim-uam.mysql.url}")
    private String url;
    @Value("${kim-uam.mysql.username}")
    private String username;
    @Value("${kim-uam.mysql.password}")
    private String password;

    @Autowired
    private KimUserFriendsService userFriendsService;

    public static Integer isDo = 0;

    public static boolean isGenerateFriends = false;

    /**
     * 用户同步定时任务
     *
     * @return
     */
    @Scheduled(cron = "0 0/1 * * * ?")//定时每五分钟
    public void doUserDateSynTask() {
        logger.info("LINK用户同步定时任务 S");

        if(isDo == 0) {
            try {
                ConnectionDB connectionDB = new ConnectionDB(driver, url, username, password);
                userFriendsService.doUserDateSyn(connectionDB);
                if (isGenerateFriends) {
                    isGenerateFriends = false;
                    userFriendsService.generateFriendsForTest();
                }
            }catch (Exception e){
                isDo = 0;
                e.printStackTrace();
            }

        }

        logger.info("LINK用户同步定时任务 E");
    }


}
